<?php

namespace Regard\ContactBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Regard\ContactBundle\Entity\Message;
use Regard\ContactBundle\Entity\Client;
use Regard\ContactBundle\Form\MessageType;

class ContactController extends Controller
{
    /**
     * Show and treat the contact form
     * @return render($view, array($form))
     */
    public function prepareMailAction($type, $messageContent)
    {
        $view = 'RegardContactBundle:Contact:prepareMail.html.twig';
        
        // create a new form from the object 'Message'
        $message = new Message;
        $form = $this->createForm(new MessageType,$message);
        
        // get the HTTP request
        $request = $this->get('request');
        
        // verify the method and bind the form content in the object
        if ($request->getMethod() == 'POST') {
            $form->bind($request);
             
            // treat the form content
            if ($form->isValid()) {
                $em = $this->getDoctrine()->getManager();
                $em->persist($message);
                $em->flush();
                
                // insert new client in DB
                $this->addNewClientAction($message);
                
                // send the mail containing the message
                $this->sendMailAction($message);
                        
                // redirect to avoid resubmission
                return $this->redirect($this->generateUrl('regard_contact_index'));
            }
        }
        
        // render the view with the form
        return $this->render($view, array('form' => $form->createView(),
                                        'type' => $type,
                                        'message' => $messageContent));
        
    }

    /**
     * Add a new client, if new, from the new message
     * @param Message $message
     */
    public function addNewClientAction(Message $message)
    {
        // get the new client
        $client = $this->getDoctrine()
                    ->getManager()
                    ->getRepository('RegardContactBundle:Client')
                    ->findBy(array('email' => $message->getEmail()));
        
        // if the client doesn't exist
        if (!$client) {
            
            // create a new client
            $client = new Client();

            // get client informations
            $client->setName($message->getName());
            $client->setEmail($message->getEmail());
            $client->setOptin($message->getOptin());

            // inset client in DB
            $em = $this->getDoctrine()->getManager();
            $em->persist($client);
            $em->flush();
        }
    }
    
    /**
     * Send a message to the administrator
     * @param Message $message
     */
    public function sendMailAction(Message $message)
    {
        $view = 'RegardContactBundle:Contact:mailContact.html.twig';
        
        // mail composition
        $mail = \Swift_Message::newInstance()
                ->setSubject('Nouveau message d\'un client sur parfumerieregard.com')
                ->setFrom('contact@parfumerieregard.com')
                ->setTo('contact@parfumerieregard.com')
                //->setTo('seb.fabiani@gmail.com')
                ->setBody($this->renderView($view, array('message' => $message)));
        
        // send the mail
        try {
            $this->get('mailer')->send($mail);
            $this->get('session')->getFlashBag()->add('notice', 'Votre message a bien été envoyé !');
        } catch (Swift_TransportException $e) {
            throw new \Swift_TransportException;
        }
    }
}
